#include "InputFilter.h"

int InputFilter::nInstances = 0;
int InputFilter::nSyncs = 0;
xn::Context InputFilter::context;
Filter *InputFilter::syncFilter = nullptr;

InputFilter::InputFilter(int outputsCount) : Filter(0, outputsCount)
{
	if (!nInstances)
	{
		context.Init();
		syncFilter = this;
	}
	++nInstances;
}


InputFilter::~InputFilter(void)
{
	--nInstances;
	if (!nInstances)
	{
		context.Shutdown();
		syncFilter = nullptr;
	}
}

void InputFilter::NextFrame()
{
	Filter::NextFrame();
	if (nSyncs == 0)
		context.WaitAndUpdateAll();

	nSyncs = (nSyncs + 1) % nInstances;
}

void InputFilter::UseToSynchronize()
{
	syncFilter = this;
}
